﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Caliburn.Micro;
using Coproject.Framework;
using Coproject.ViewModels.Interfaces;

namespace Coproject.ViewModels
{
	[Export(typeof(IShell))]
	public class ShellViewModel : LazyConductor<IModule, IModuleMetadata>.Collection.OneActive, IShell
	{
		[ImportingConstructor]
		public ShellViewModel([ImportMany]IEnumerable<ExportFactory<IModule, IModuleMetadata>> moduleFactories)
		{
			var modules = from h in moduleFactories orderby h.Metadata.Order select LazyScreen.Create(h);
			Items.AddRange(modules);

			this.PropertyChanged += (s, e) =>
				{
					if (e.PropertyName == "ActiveItem")
					{
						NotifyOfPropertyChange(() => CanCloseActiveItem);

					}
				};
		}

		public bool CanCloseActiveItem
		{
			get
			{
				return ActiveItem != null;
			}
		}

		public void CloseActiveItem()
		{
			DeactivateItem(ActiveItem, true);
		}
	}
}
